package com.example.demo.interceptor;

import javax.servlet.*;

import javax.servlet.annotation.WebFilter;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WJ
 * Date: 2023-02-19
 * Time: 10:34
 */
// 配置跨域过滤器
@WebFilter(urlPatterns = "/*", filterName = "AppFilter")
public class AppFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws ServletException, IOException {

        HttpServletRequest req = (HttpServletRequest) servletRequest;

        HttpServletResponse resp = (HttpServletResponse) servletResponse;

        String origin = req.getHeader("Origin");

        if(origin == null) {

            origin = req.getHeader("Referer");

        }

        resp.setHeader("Access-Control-Allow-Origin", origin);//这里不能写*，*代表接受所有域名访问，如写*则下面一行代码无效。谨记

        resp.setHeader("Access-Control-Allow-Credentials", "true");//true代表允许携带cookie

        chain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {

    }
}
